﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Windows;
using System.Windows.Input;
using SLS.ExClassLib.Event;
using SLS.ExClassLib.Events;
using SLS.ExClassLib.Message;
using SLS.ExClassLib.Database;
using SLS.ExClassLib.Data;
using SLS.ExClassLib.WFActivities;
namespace SLS.ExClassLib.MVVM
{

    /// <summary>
    /// The atm wpf view model.
    /// </summary>
    public partial class DataModel 
    {
        private ArrayList DataList
        {
            get { return DataAccessService.Instance.DataList; }
        }
        #region Methods
        public void SendMessage(MessageEx msg)
        {
            try
            {
                EventDispatcher.Instance.Publish(msg, null, EventType.WF2UI);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        public override void Write(string message)
        {

        }
        public override void WriteLine(string message)
        {

        }
        /// <summary>
        /// The requery commands.
        /// </summary>
        private static void RequeryCommands()
        {
            if (Application.Current != null)
            {
                Application.Current.Dispatcher.BeginInvoke((Action)CommandManager.InvalidateRequerySuggested);
            }
        }

        /// <summary>
        /// Determines if the InsertCard command is enabled
        /// </summary>
        /// <param name="commandData">
        /// The command Data.
        /// </param>
        /// <returns>
        /// true if the command is enabled, false if not
        /// </returns>
        private bool CanMachineJoin(object commandData)
        {
            return this.WFMachine.IsCurrentState(MachineState.Initialize);
        }
        /// <summary>
        /// Determines if the InsertCard command is enabled
        /// </summary>
        /// <param name="commandData">
        /// The command Data.
        /// </param>
        /// <returns>
        /// true if the command is enabled, false if not
        /// </returns>
        private bool CanExecuteAssignNewJob(object commandData)
        {
            return this.WFMachine.IsCurrentState(MachineState.Working);
        }

        private bool CanExecuteQualityCheck(object commandData)
        {
            return this.WFMachine.IsCurrentState(MachineState.GoCheck);
        }

        /// <summary>
        /// Determines if the KeyPad command is enabled


        /// <summary>
        /// Determines if the PowerOff command is enabled
        /// </summary>
        /// <param name="commandData">
        /// The command Data.
        /// </param>
        /// <returns>
        /// true if the command is enabled, false if not
        /// </returns>
        private bool CanExecutePowerOff(object commandData)
        {
            return this.WFMachine.PowerOn;
        }

        /// <summary>
        /// Determines if the PowerOn command is enabled
        /// </summary>
        /// <param name="commandData">
        /// The command Data.
        /// </param>
        /// <returns>
        /// true if the command is enabled, false if not
        /// </returns>
        private bool CanExecutePowerOn(object commandData)
        {
            return !this.WFMachine.PowerOn;
        }

        /// <summary>
        /// The PowerOff.
        /// </summary>
        /// <param name="commandData">
        /// The command data.
        /// </param>
        private void ExecutePowerOff(object commandData)
        {
            this.WFMachine.TurnPowerOff();
        }
        #endregion 

        public void LoadProcessing(string basetable)
        {
            try
            {
                TableBase.Clear();
                DataSet setEvent = new DataSet();
                foreach (ExDataRow row in DataList)
                {
                    setEvent = (DataSet)DataAccessService.Instance.ImportDataTable(setEvent, row.Item);
                }
                SetEnv.Merge(setEvent);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        public virtual DataTable SetUpTableBase(DataSet setEvent, bool CheckDataQuality)
        {
            try
            {
                if (TableBase != null)
                {
                    TableBase.Clear();
                }
                if (setEvent == null)
                    return TableBase;
                if (setEvent.Tables.Contains(BaseTable))
                { 
                    SetEnv.Tables[BaseTable].Clear();
                } 
                SetEnv.Merge(setEvent.Tables[BaseTable]);
                foreach (DataRow dwEvent in setEvent.Tables[BaseTable].Rows)
                {
                    ExDataRow exd = new ExDataRow();
                    exd.GetExDataRow(dwEvent, true, CheckDataQuality);
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return TableBase;
        }
        
        public void Clear(DataRow dwThis, bool RemoveFromDataList = false)
        {
            try
            {
                ExDataRow exd = new ExDataRow();
                exd.GetExDataRow(dwThis, false);
                ExDataRow deleteone = null;
                string INSTANCEID = dwThis["INSTANCEID"].ToString();
                var query = from ExDataRow datalist in DataList
                            where datalist.INSTANCEID == INSTANCEID
                            select datalist;
                foreach (ExDataRow exdw in query)
                {
                    exdw.Clear();
                    SetEnv.Clear();
                    deleteone = exdw;
                }

                if (RemoveFromDataList && deleteone != null)
                {
                    DataList.Remove(deleteone);
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        public void ExecuteKeyRelaitonComplete(DataSet setEvent)
        {
            DataRow dwEvent = null;
            try
            {
                if (setEvent == null)
                    return;
                if (setEvent.Tables.Count == 0)
                    return;
                if (setEvent.Tables[0].Rows.Count == 0)
                    return;
                dwEvent = setEvent.Tables[0].Rows[0];
                string INSTANCEID = dwEvent["INSTANCEID"].ToString().Trim();
                var query = from ExDataRow datalist in DataList
                            where datalist.INSTANCEID == INSTANCEID
                            select datalist;
                if (query.Count() > 0)
                {
                    foreach (ExDataRow exd in query)
                    {
                        exd.SUBSTATUS = dwEvent["SUBSTATUS"].ToString();
                        exd.Item["SUBSTATUS"] = dwEvent["SUBSTATUS"];
                    }
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }

        }
        public DataRow GetRecord(string INSTANCEID)
        {
            DataRow dwEvent = null;
            try
            {
                ExDataRow exdwThis = null;
                var query = from ExDataRow datalist in DataList
                            where datalist.INSTANCEID == INSTANCEID
                            select datalist;
                foreach (ExDataRow exRow in query)
                {
                    exdwThis = exRow;

                }
                if (exdwThis == null)
                    return null;
                dwEvent = exdwThis.Item;
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return dwEvent;
        } 
        public virtual void UpdateTable2Create(DataRow dwThis, string TBName, DataTable TBThis, bool blCreated = true)
        {
            try
            {
                string INSTANCEID = dwThis["INSTANCEID"].ToString();
                var query = from ExDataRow datalist in DataList
                            where datalist.INSTANCEID == INSTANCEID
                            select datalist;
                foreach (ExDataRow exdw in query)
                {
                    exdw.AddSet2Create(TBName, TBThis, blCreated);
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        /// <summary>
        /// The PowerOn.
        /// </summary>
        /// <param name="commandData">
        /// The command data.
        /// </param>
        public void ExecutePowerOn(object commandData)
        {
            if (WFMachine == null)
                return;
            this.WFMachine.TurnPowerOn((bool)commandData);
        }

        /// <summary>
        /// Notify that a property has changed
        /// </summary>
        /// <param name="property">
        /// The property that changed
        /// </param>
        private void NotifyChanged(string property)
        {
            if (this.PropertyChanged != null)
            {
                this.PropertyChanged(this, new PropertyChangedEventArgs(property));
            }
        }

        public void ExecuteMachineReady(bool IsReady)
        {
            if (WFMachine == null)
                return;

            this.WFMachine.MachineReady(IsReady);
        }

        /// <summary>
        /// The InsertCard.
        /// </summary>
        /// <param name="commandData">
        /// The command data.
        /// </param>
        public void ExecuteMachineJoined(object commandData)
        {
            if (WFMachine == null)
                return;
            this.WFMachine.MachineReady(Convert.ToBoolean(commandData));
        }

        /// <summary>
        /// Determines if the InsertCard command is enabled
        /// </summary>
        /// <param name="commandData">
        /// The command Data.
        /// </param>
        /// <returns>
        /// true if the command is enabled, false if not
        /// </returns>
        public void ExecuteAssignNewJob(MessageEx msg, bool IsCheckDataQuality)
        {
            if (WFMachine == null)
                return;
            this.WFMachine.ExecuteAssignNewJob((MessageEx)msg, IsCheckDataQuality);
        }

        /// <summary>
        /// Determines if the InsertCard command is enabled
        /// </summary>
        /// <param name="commandData">
        /// The command Data.
        /// </param>
        /// <returns>
        /// true if the command is enabled, false if not
        /// </returns>
        public void ExecuteQualityCheck(object commandData)
        {
            if (WFMachine == null)
                return;
            this.WFMachine.ExecuteQualityCheck((bool)commandData);
        }
 
    }
}